package com.cao.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cao.common.core.domain.model.LoginUser;
import com.cao.common.utils.SecurityUtils;
import com.cao.project.domain.Project;
import com.cao.project.domain.ProjectMember;
import com.cao.project.domain.Task;
import com.cao.project.mapper.ProjectMapper;
import com.cao.project.mapper.ProjectMemberMapper;
import com.cao.project.mapper.TaskMapper;
import com.cao.project.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目：存储项目信息Service业务层处理
 *
 * @author cao
 * @date 2025-03-03
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
    @Autowired
    private ProjectMemberMapper projectMemberMapper;
    @Autowired
    private TaskMapper taskMapper;

    /**
     * 查询项目：存储项目信息列表
     *
     * @param project 项目：存储项目信息
     * @return 项目：存储项目信息
     */
    @Override
    public List<Project> selectProjectList(Project project) {
        return baseMapper.selectProjectList(project);
    }

    @Override
    public List<Project> getMyProject(Project project) {
        Long userId = SecurityUtils.getUserId();
        List<ProjectMember> projectMembers = projectMemberMapper.selectList(new LambdaQueryWrapper<ProjectMember>().eq(ProjectMember::getUserId, userId));
        return baseMapper.selectProjectList(project)
                .stream()
                .filter(p -> projectMembers.stream().anyMatch(pm -> pm.getProjectId().equals(p.getId()))).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public int add(Project project) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        project.setCreatedBy(loginUser.getUserId());
        project.setCreatedName(loginUser.getUsername());
        baseMapper.insert(project);
        ProjectMember projectMember = new ProjectMember();
        projectMember.setProjectId(project.getId());
        projectMember.setUserId(loginUser.getUserId());
        projectMember.setUserName(loginUser.getUsername());
        projectMember.setProjectName(projectMember.getProjectName());
        projectMember.setRole("creator");//创建者
        projectMemberMapper.insert(projectMember);
        return 1;
    }

    @Override
    @Transactional
    public int del(List<Long> list) {
        for (Long id : list) {
            List<ProjectMember> projectMembers = projectMemberMapper.selectList(new LambdaQueryWrapper<ProjectMember>().eq(ProjectMember::getProjectId, id));
            if (!projectMembers.isEmpty()) throw new RuntimeException("该项目下还有成员，无法删除");
            List<Task> tasks = taskMapper.selectList(new LambdaQueryWrapper<Task>().eq(Task::getProjectId, id));
            if (!tasks.isEmpty()) throw new RuntimeException("该项目下还有任务，无法删除");
        }
        return baseMapper.deleteBatchIds(list);
    }
}
